<?php
require_once('AbstractController.php');
include_once ("arc/ARC2.php");
/**
 * AuthorController.php
 *
 * @author		b.wenneker <b.wenneker@gmail.com>, 2010
 */
class LocationController extends Thesis_AbstractController
{
	
	public function overviewAction(){
				
		$store = $this->_getStore();
		$query = 'select ?location where {
		 	?d <http://dbpedia.org/ontology/Place> ?uri .
			?uri <http://www.w3.org/2002/07/owl#sameAs> ?location
		}';
		$rows = $store->query($query, 'rows');
		
		$store = $this->_getStore(8892);
		$locationContinents = array();
		$continentCounts = array();
		foreach ((array) $rows as $row) {
			
			if(isset($locationContinents[$row['location']])){
				$continentCounts[$locationContinents[$row['location']]] += 1;
				continue;
			}
			
			$query = sprintf(
				'PREFIX gn: <http://www.geonames.org/ontology#>
				select ?name where {
				  {
				      <%s> gn:parentFeature ?country_uri .
				      ?country_uri gn:parentFeature ?continent_uri .
				      ?continent_uri gn:name ?name
				  }UNION{
				      <%s> gn:parentFeature ?region_uri .
				      ?region_uri gn:parentFeature ?country_uri .
				      ?country_uri gn:parentFeature ?continent_uri .
				      ?continent_uri gn:name ?name
				  }
				  ?continent_uri gn:featureCode ?c .
				  FILTER(regex(?c,"CONT"))
				} ORDER BY ?name',
				$row['location'],
				$row['location']
			);
			$tmpRows = $store->query($query, 'rows');
			if($tmpRows){
				$locationContinents[$row['location']] = $tmpRows[0]['name'];
				if(!isset($continentCounts[$locationContinents[$row['location']]] )){
					$continentCounts[$locationContinents[$row['location']]] = 1;
				}else{
					$continentCounts[$locationContinents[$row['location']]] += 1;
				}
			}
		}
		
		ksort($continentCounts);
				
		$this->view->assign('continentCounts', $continentCounts);
	}
	
	public function indexAction(){
		
		$store = $this->_getStore();
		$query = 'select ?location ?title ?place where {
				?d <http://purl.org/dc/elements/1.1/title> ?title .
				?d <http://dbpedia.org/ontology/Place> ?uri .
				?uri <http://www.w3.org/2000/01/rdf-schema#label> ?place .
				?uri <http://www.w3.org/2002/07/owl#sameAs> ?location
			} ORDER BY ?title';
		$rows = $store->query($query, 'rows');

		$continent = $this->getRequest()->getParam('queryparam');
		
		$store = $this->_getStore(8892);
		$locationContinents = array();

		$publications = array();
		foreach ((array) $rows as $row) {
			
			if(isset($locationContinents[$row['location']])){
				
				if($locationContinents[$row['location']] == $continent){
					$publications[] = $row['title'] . '|'. $row['place'];
				}
				continue;
			}
			
			$query = sprintf(
				'PREFIX gn: <http://www.geonames.org/ontology#>
				select ?name where {
				  {
				      <%s> gn:parentFeature ?country_uri .
				      ?country_uri gn:parentFeature ?continent_uri .
				      ?continent_uri gn:name ?name
				  }UNION{
				      <%s> gn:parentFeature ?region_uri .
				      ?region_uri gn:parentFeature ?country_uri .
				      ?country_uri gn:parentFeature ?continent_uri .
				      ?continent_uri gn:name ?name
				  }
				  ?continent_uri gn:featureCode ?c .
				  FILTER(regex(?c,"CONT"))
				} ORDER BY ?name',
				$row['location'],
				$row['location']
			);
			$tmpRows = $store->query($query, 'rows');
			if($tmpRows){
				$locationContinents[$row['location']] = $tmpRows[0]['name'];
				if($locationContinents[$row['location']] == $continent){
					$publications[] = $row['title'] . '|'. $row['place'];
				}
				
			}
		}
		
		$this->view->assign('continent', $continent);
		$this->view->assign('titles', $publications);
	}
}